package org.edu.dao.impl;

import java.util.List;

import org.edu.dao.StudentLessonDao;
import org.edu.model.StudentLesson;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class StudentLessonDaoImpl implements StudentLessonDao {

	@Autowired
	private SessionFactory sessionFactory;
	public StudentLessonDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void save(StudentLesson studentLesson) throws Exception {
		sessionFactory.getCurrentSession().save(studentLesson);
	}

	@Override
	public void update(StudentLesson studentLesson) throws Exception {
		sessionFactory.getCurrentSession().update(studentLesson);
	}

	@Override
	public void remove(StudentLesson studentLesson) throws Exception {
		sessionFactory.getCurrentSession().delete(studentLesson);
	}

	@Override
	public List<StudentLesson> studentLessons() {
		return sessionFactory.getCurrentSession().createQuery("from StudentLesson").list();
	}

	@Override
	public StudentLesson findById(Long student_lesson_id) throws Exception {
		return (StudentLesson) sessionFactory.getCurrentSession().load(StudentLesson.class, student_lesson_id);
	}

	@Override
	public List<StudentLesson> myLessonList(Long id) throws Exception {
//		String sql = "from StudentLesson where student_id = 2";
//		Query query = sessionFactory.getCurrentSession().createQuery(sql);
//		query.setParameter("student_id", "2");
//		return (List<StudentLesson>) query.uniqueResult();
		Query query = sessionFactory.getCurrentSession().createQuery("FROM StudentLesson WHERE student_id = :student_id");
		query.setParameter("student_id", id); 
		List<StudentLesson> qry = query.list();
		return qry;
//		return sessionFactory.getCurrentSession().createQuery("from StudentLesson where student_id = 2").list();
	}
}
